using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace CaltechSoft.Service.Infrastructure.Utility
{
    public class EnumUtilities
    {
        public static IList<SelectListItem> BuildSelectList<T>(T? selectedValue) where T : struct
        {
            IList<SelectListItem> items = new List<SelectListItem>();
            Array values = Enum.GetValues(typeof(T));
            foreach (object value in values)
            {
                string description = (value as Enum).ToDescriptionString();
                SelectListItem item = new SelectListItem
                                          {
                                              Value = Convert.ToInt32(value).ToString(),
                                              Text = string.IsNullOrEmpty(description) ? Enum.GetName(typeof(T), value) : description
                                          };
                if (selectedValue.HasValue && selectedValue.Value.Equals(value)) item.Selected = true;
                items.Add(item);
            }
            return items;
        }

        public static IList<SelectListItem> BuildSelectList<T>(T? selectedValue, bool hasEmptyValue) where T : struct
        {
            var items = BuildSelectList<T>(selectedValue);
            if (hasEmptyValue)
            {
                var emptyValue = new SelectListItem
                                     {
                                         Text = "",
                                         Value = "",
                                         Selected = selectedValue == null ? true : false
                                     };
                items.Insert(0, emptyValue);
            }
            return items;
        }

        public static string GetTextOfSelectedItem<T>(int selectedValue) where T : struct
        {
            Array values = Enum.GetValues(typeof(T));
            foreach (object value in values)
            {
                string description = (value as Enum).ToDescriptionString();
                if (selectedValue.Equals(value.GetHashCode())) return string.IsNullOrEmpty(description) ? Enum.GetName(typeof(T), value) : description;
            }
            return string.Empty;
        }

        public static IList<SelectListItem> BuildEnumSelectList<T>(T? selectedValue) where T : struct
        {
            IList<SelectListItem> items = new List<SelectListItem>();
            Array values = Enum.GetValues(typeof(T));
            foreach (object value in values)
            {
                string description = (value as Enum).ToDescriptionString();
                SelectListItem item = new SelectListItem
                {
                    Value = value.ToString(),
                    Text = string.IsNullOrEmpty(description) ? Enum.GetName(typeof(T), value) : description
                };
                if (selectedValue.HasValue && selectedValue.Value.Equals(value)) item.Selected = true;
                items.Add(item);
            }
            return items;
        }

        public static T[] GetEnumValues<T>() where T : struct
        {
            if (!typeof(T).IsEnum) throw new ArgumentException("GetValues<T> can only be called for types derived from System.Enum", "T");
            return (T[])Enum.GetValues(typeof(T));
        }
    }
}